package com.utils;

import java.io.InputStream;
import java.sql.*;
/*import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;*/
import java.util.Properties;

import com.bean.FilterPassageCondition;

public class JDBCUtils {
	public static void release(ResultSet rs, 
			Statement statement, Connection conn) {
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	

	public static void release(Statement statement, Connection conn) {
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

	public static void release(ResultSet rs, 
			PreparedStatement ps, Connection conn) {
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		if (ps != null) {
			try {
				ps.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	public static Connection getConnection() throws Exception {
	
		Properties properties = new Properties();


		InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream(
				"jdbc.properties");


		properties.load(in);


		String user = properties.getProperty("user");
		String password = properties.getProperty("password");
		String url = properties.getProperty("url");
		String driver = properties.getProperty("driver");


		Class.forName(driver);


		return DriverManager.getConnection(url, user, password);
		
	}
	
    /*******************************************************************
     *          以下为资源销毁模块 
     ******************************************************************/
    
    public static void close(ResultSet rs)
    {
    	try
    	{
    		if(rs!=null)  //规避了空指针
    		{
    			rs.close();
    		}
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    }
    
    public static void close(PreparedStatement pstm)
    {
    	try
    	{
    		if(pstm!=null)  //规避了空指针
    		{
    			pstm.close();
    		}
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    }


}
